<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_logo">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form
        ref="loginForm"
        :model="loginData"
        :rules="loginRules"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="adminname">
          <el-input v-model="loginData.adminname" prefix-icon="el-icon-user-solid" clearable></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginData.password" prefix-icon="el-icon-view" type="password" clearable></el-input>
        </el-form-item>
        
        <el-form-item class="login_btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      loginData: {
        adminname: "",
        password: ""
      },
      loginRules: {
        adminname: [
          { required: true, message: "请输入管理员名", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    resetLogin() {
      this.loginData.adminname = "";
      this.loginData.password = "";
    },
    login() {
      this.$refs.loginForm.validate(valid => {
        if (!valid) return;
        else {
          axios.post("/admins/login", this.loginData).then(res => {
            if (res.data) {
              this.$message({
                showClose: true,
                message: "登录成功",
                type: "success"
              });
              this.$router.push("/orderformStatistics");
            } else {
              this.$message({
                showClose: true,
                message: "登录账号错误，请重新输入",
                type: "error"
              });
            }
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.login_container {
  background-color: rgb(64, 144, 110);
  height: 820px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login_box {
  width: 500px;
  height: 350px;
  background-color: #fff;
  border-radius: 10px;
}
.login_logo {
  height: 150px;
  width: 150px;
  margin: 0 auto;
}
.login_logo img {
  width: 100%;
  height: 100%;
  border-radius: 30%;
}
.login_form {
  box-sizing: border-box;
  padding: 15px;
}
.login_btn {
  display: flex;
  justify-content: flex-end;
}
</style>